package com.itheima.test;

import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest1 {

    @Autowired
    private UserMapper userMapper;


    // 新增记录
    @Test
    public void test01()throws Exception{
        User user = new User();
        user.setId(99L); // 可以手动指定主键
        user.setUsername("zhangsan2");
        user.setPassword("123456");
        user.setNickName("张三");
        user.setAge(30);
        user.setEmail("hahah@qq.com");

        userMapper.insert(user);
    }

    // 更新记录【支持动态sql】  UPDATE tb_user SET email=? WHERE id=?
    @Test
    public void test02()throws Exception{
        User user = new User();
        user.setId(99L);
        user.setEmail("jiujiu@qq.com");
        userMapper.updateById(user);
    }


    // 根据id查询一个  SELECT id,username,password,nick_name,age,email FROM tb_user WHERE id=?
    @Test
    public void test03()throws Exception{
        User user = userMapper.selectById(99L);
        System.out.println(user);
    }

    // 根据多个id查询  SELECT id,username,password,nick_name,age,email FROM tb_user WHERE id IN ( ? , ? )
    @Test
    public void test04()throws Exception{
        List<Long> idList = new ArrayList<>();
        idList.add(1L);
        idList.add(99L);
        List<User> list = userMapper.selectBatchIds(idList);
        System.out.println(list);
    }

    // 根据id删除一个  DELETE FROM tb_user WHERE id=?
    @Test
    public void test05()throws Exception{
        userMapper.deleteById(1L);
    }

    // 根据多个id删除（此代码就不知行了...）  DELETE FROM tb_user WHERE id IN( ? , ? )
    @Test
    public void test06()throws Exception{
        List<Long> idList = new ArrayList<>();
        idList.add(1L);
        idList.add(99L);
        userMapper.deleteBatchIds(idList);
    }

    // 查询所有  SELECT id,username,password,nick_name,age,email FROM tb_user
    @Test
    public void test07()throws Exception{
        List<User> list = userMapper.selectList(null);
        System.out.println(list);
    }
}
